//
//  ByLoginViewModel.m
//  Homept_ios
//
//  Created by mc on 16/9/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByLoginViewModel.h"

@implementation ByLoginViewModel
#pragma mark-----登录
- (void)login:(ByNetParamsModel *)model {
    if ([model.username isStringNull]) {
        [ByCoreToolAlert showOnlyText:@"请输入手机号"];;
        return;
    } else if([model.password isStringNull]){
        [ByCoreToolAlert showOnlyText:@"请输入密码"];
        return;
    }else if (model.username.length != 11) {
        [ByCoreToolAlert showOnlyText:@"请输入正确手机号"];
        return ;
    }
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.loginDelegate && [that.loginDelegate respondsToSelector:@selector(loginFail:)]) {
        
        failure = ^(ByNetBaseResult *result){
            [that.loginDelegate loginFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi login:model success:^(ByNetBaseResult *result) {
        
        if (that.loginDelegate) {
            [that.loginDelegate loginSuccess:result];
        }
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}

#pragma mark-----注册
- (void)registe:(ByNetParamsModel *)model {
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.registerDelegate && [that.registerDelegate respondsToSelector:@selector(registerFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.registerDelegate registerFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi reigster:model success:^(ByNetBaseResult *result) {
        if (that.registerDelegate) {
            [that.registerDelegate registerSuccess:result];
        }
    } failure:failure];
}
#pragma mark-----发送验证码
- (void)fetchCode:(ByNetParamsModel *)model{
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.fetchcodeDelegate && [that.fetchcodeDelegate respondsToSelector:@selector(fetchCodeFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.fetchcodeDelegate fetchCodeFail:result];
        };
    }
    // 请求网络
    [ByNetCodeApi getCode:model success:^(ByNetBaseResult *result) {
        if (that.fetchcodeDelegate) {
            [that.fetchcodeDelegate fetchCodeSuccess:result];
        }
    } failure:failure];
}
#pragma mark-----验证验证码
- (void)checkCode:(ByNetParamsModel *)model{
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.checkcodeDelegate && [that.checkcodeDelegate respondsToSelector:@selector(checkCodeFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.checkcodeDelegate checkCodeFail:result];
        };
    }
    // 请求网络
    [ByNetCodeApi checkCode:model success:^(ByNetBaseResult *result) {
        if (that.checkcodeDelegate) {
            [that.checkcodeDelegate checkCodeSuccess:result];
        }
    } failure:failure];

}


#pragma mark --------个人资料更新
//个人资料更新
- (void)updatePersonData:(ByNetParamsModel *)model {
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.personUpdateDelegate && [that.personUpdateDelegate respondsToSelector:@selector(personUpdateFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.personUpdateDelegate personUpdateFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi updateUserInfo:model success:^(ByNetBaseResult *result) {
        if (that.personUpdateDelegate) {
            [that.personUpdateDelegate personUpdateSuccess:result];
        }
    } failure:failure];
}

#pragma mark --------绑定手机
//绑定手机
- (void)boundMobile:(ByNetParamsModel *)model {
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.boundMobileDelegate && [that.boundMobileDelegate respondsToSelector:@selector(boundMobileFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.boundMobileDelegate boundMobileFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi boundMobilePhone:model success:^(ByNetBaseResult *result) {
        if (that.boundMobileDelegate) {
            [that.boundMobileDelegate boundMobileSuccess:result];
        }
    } failure:failure];
}
#pragma mark --------上传头像
//上传头像
- (void)upLoadMyIcon:(UIImage *)icon {
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.iconUpLoadDelegate && [that.iconUpLoadDelegate respondsToSelector:@selector(iconUpFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.iconUpLoadDelegate iconUpFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi iconUpload:icon success:^(ByNetBaseResult *result) {
        if (that.iconUpLoadDelegate) {
            [that.iconUpLoadDelegate iconUpSuccess:result];
        }
    } failure:failure];
}

#pragma mark --------找回密码
// 找回密码
- (void)findBackPassword:(ByNetParamsModel *)model {
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.findBackPswDelegate && [that.findBackPswDelegate respondsToSelector:@selector(findBackPswFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.findBackPswDelegate findBackPswFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi retrievePwd:model success:^(ByNetBaseResult *result) {
        if (that.findBackPswDelegate) {
            [that.findBackPswDelegate findBackPswSuccess:result];
        }
    } failure:failure];
}

#pragma mark --------实名认证
// 实名认证
// 第一步
- (void)uploadIDImages:(NSArray *)images {
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.realNameAttestationDelegate && [that.realNameAttestationDelegate respondsToSelector:@selector(uploadIDImagesFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.realNameAttestationDelegate uploadIDImagesFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi realNameAttestation:images success:^(ByNetBaseResult *result) {
        if (that.realNameAttestationDelegate) {
            [that.realNameAttestationDelegate uploadIDImagesSuccess:result];
        }
    } failure:failure];
}
// 第二步
- (void)realNameAttestation:(ByNetParamsModel *)model {
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.realNameAttestationDelegate && [that.realNameAttestationDelegate respondsToSelector:@selector(realNameAttestationFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.realNameAttestationDelegate realNameAttestationFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi realNameAttestationNext:model success:^(ByNetBaseResult *result) {
        if (that.realNameAttestationDelegate) {
            [that.realNameAttestationDelegate realNameAttestationSuccess:result];
        }
    } failure:failure];
}

#pragma mark --------获取用户信息
// 获取用户信息
- (void)getNewUserInfo:(ByNetParamsModel *)model {
    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.getUserNewInfoDelegate && [that.getUserNewInfoDelegate respondsToSelector:@selector(getUserNewInfoFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.getUserNewInfoDelegate getUserNewInfoFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi getUserInfo:model success:^(ByNetBaseResult *result) {
        if (that.getUserNewInfoDelegate) {
            [that.getUserNewInfoDelegate getUserNewInfoSuccess:result];
        }
    } failure:failure];

}


#pragma mark --------提交意见反馈
// 提交意见反馈
- (void)feedback:(ByNetParamsModel *)model {

    void (^failure)(ByNetBaseResult *result) = nil;
    __weak ByLoginViewModel *that = self;
    if(that.feedbackDelegate && [that.feedbackDelegate respondsToSelector:@selector(feedbackFail:)]) {
        failure = ^(ByNetBaseResult *result){
            [that.feedbackDelegate feedbackFail:result];
        };
    }
    // 请求网络
    [ByNetUserApi feedback:model success:^(ByNetBaseResult *result) {
        if (that.feedbackDelegate) {
            [that.feedbackDelegate feedbackSuccess:result];
        }
    } failure:failure];

}

@end
